//开始培训
if ($.cookie("staffid") != null) {
    var json = eval(getwebdata($.cookie('jobsId'), '1', 'sel_video', 2));
    var num_id = $.cookie('sectionID');
    var kspx_jd = eval(getwebdata($.cookie('staffid'), "1", "get_kspx_num", 2));
    var kspxsp_num = kspx_jd[0].kspxsp_num;
    var myPlayer;
    if (parseInt(kspxsp_num) == 0) {
        var sectionID = json[0].sectionID;
    } else {
        var sectionID = parseInt(kspxsp_num) + 1;
    }
    var js = 1;
    var timeid = eval(getwebdata($.cookie('staffid') + "###" + sectionID, '1', 'u_control_i', 2))[0].id;
    $(document).ready(function () {
        //time();
        ks_lesson = eval(getwebdata($.cookie("jobsId") + "###" + sectionID, "1", "sel_Article_context", 2));
        var src = ks_lesson[0].context.substring(ks_lesson[0].context.indexOf('http//') + 4, ks_lesson[0].context.indexOf('.mp4') + 4);
        $("#ks_type").html(json[num_id].sectionname);
        $("#teacher").html(ks_lesson[0].teacher);
        $("#vedio_src,#example_video_1_html5_api,#example_video_1_flash_api").prop("src", "" + src + "");
        //var options = { techOrder: ["html5", "flash"], autoplay: true, preload: true, bigPlayButton: true, controls: true };

        //myPlayer = videojs('example_video_1', options, function onPlayerReady() {
        //    //videojs.log('Your player is ready!');

        //    //// In this context, `this` is the player that was created by Video.js.
        //    //this.play();

        //    // How about an event listener?
         
        //});
        myPlayer = videojs("example_video_1", {
            techOrder: ["html5", "flash"],
            "controls": true,
            "autoplay": true,
            "preload": "auto",
            "bigPlayButton": true,
            "controlBar": {
                "fullscreenToggle": true,
                "progressControl": false
            }
        });
        myPlayer.on('timeupdate', function () {
            // 如果 currentTime() === duration()，则视频已播放完毕
            js = js + 1;
            if (myPlayer.duration() != 0 && myPlayer.currentTime() === myPlayer.duration()) {
                // 播放结束
                subPxspNum();
            }
            else if (js % 50 == 0) {
                saveplaytime();
                js = 1;
            }
        });
        //myPlayer = videojs("example_video_1",{
        //    techOrder : ["html5","flash"],
        //    "controls": true, 
        //    "autoplay": true,
        //    "preload": "auto",
        //    "bigPlayButton": true,
        //    "controlBar": {
        //        "fullscreenToggle": true,
        //        "progressControl" : false
        //    },
        //    function onPlayerReady() {
        //    });
        //});
        if (sectionID > kspxsp_num) {
            $(".vjs-play-control,.vjs-progress-control,.vjs-mute-control,.vjs-time-divider").remove();
            $(".vjs-volume-control").css("margin-right", "20px");
        }
        // 播放
        $("#play").click(function () {
            myPlayer.play();
            getwebdata(timeid, "1", "play_jl_date", 2);
            alert("已开始播放");
        });
        // 暂停
        $("#pause").click(function () {
            var currentTime = myPlayer.currentTime();
            if (myPlayer.duration() != 0 && myPlayer.currentTime() != myPlayer.duration()) {
                saveplaytime();
                 }
            else { alert("该视频已全部看完，无需保存进度了"); }
            myPlayer.pause();
        });
        // 前进至上一次进度
        $("#currentTime").click(function () {
            var userid = $.cookie('staffid');
            var json = eval(getwebdata(userid + "###" + sectionID, '1', 'sel_spTime', 2));
            if (confirm("当前已播放时间为" + parseInt(myPlayer.currentTime() / 60) + ",之前的保存进度为:" + parseInt(json[0].time / 60) + "分钟,是否前进至之前观看时间") == true)
                myPlayer.currentTime(json[0].time);
        });
        // 保存进步
        $("#save").click(function () {
            var currentTime = myPlayer.currentTime();
            if (myPlayer.duration() != 0 && myPlayer.currentTime() != myPlayer.duration()) {
                saveplaytime();
                  }
            else { alert("该视频已全部看完，无需保存进度了"); }
          
        });
        // 返回列表
        $("#btn_next_list").click(function (event) {
            getwebdata(timeid, "1", "u_d_contorl", 2);
            if (sectionID > kspxsp_num) {
                if (parseInt(myPlayer.currentTime()) == parseInt(myPlayer.duration()) && myPlayer.currentTime() > 0) {
                    subPxspNum();
                    window.location.href = "../../indInfo/trainCourses/trainCourses.html";
                } else {
                    if (confirm('您未看完该视频，是否保存当前进度后返回列表') == true) {
                        saveplaytime();
                        window.location.href = "../../indInfo/trainCourses/trainCourses.html";
                    } else {
                        window.location.href = "../../indInfo/trainCourses/trainCourses.html";
                    }
                }
            } else {
                window.location.href = "../../indInfo/trainCourses/trainCourses.html";
            }
        });
        // 返回列表
        $("#back").click(function (event) {
            getwebdata(timeid, "1", "u_d_contorl", 2);
            if (sectionID > kspxsp_num) {
                if (parseInt(myPlayer.currentTime()) == parseInt(myPlayer.duration()) && myPlayer.currentTime() > 0) {
                    subPxspNum();
                    window.location.href = "../../indInfo/trainCourses/trainCourses.html";
                } else {
                    if (confirm('您未看完该视频，是否保存当前进度后返回列表') == true) {
                        $("#save").click();
                        window.location.href = "../../indInfo/trainCourses/trainCourses.html";
                    } else {
                        window.location.href = "../../indInfo/trainCourses/trainCourses.html";
                    }
                }
            } else {
                window.location.href = "../../indInfo/trainCourses/trainCourses.html";
            }
        });
      //  end_tj()
    });
    //保存进度
    function saveplaytime() {
        var currentTime = myPlayer.currentTime();
        if (myPlayer.duration() != 0 && myPlayer.currentTime() != myPlayer.duration()) {
            var userid = $.cookie('staffid');
            getwebdata(userid + "###" + sectionID + "###" + parseInt(currentTime) + "###" + currentTime, '1', "savespjd", 2);
        }
    };
    // 视频播放完之后处理事件
    function subPxspNum() {
        var pxspnum = eval(getwebdata(sectionID + "###" + $.cookie('staffid'), "1", "set_kspxsp_num_proc", 2));
        if (pxspnum[0].error == 0) {
            if (pxspnum[0].playend ==1)
            {
                if ($.cookie('jobsId') == 10) {
                    alert("恭喜您完成了所有的课时视频培训!" + "\n" + '您的证书编号为:' + pxspnum[0].zs_num + '')
                    window.location.href = "../../indInfo/trainCourses/trainCourses.html";
                }
                else if ($.cookie('jobsId') == 19)
                {
                    alert("恭喜您完成了所有的课时视频培训!");
                    if (confirm("你已学完全部课时，是否立即考试") == true) {
                        $.cookie('dl-style', '2', { expires: 1 });
                        window.top.location.href = '../../login.html';
                    } else {
                        window.location.href = "../../indInfo/trainCourses/trainCourses.html";
                    }
                }
            }
            else
            {
                alert("本节视频播放完成，点击返回列表！")
                window.location.href = "../../indInfo/trainCourses/trainCourses.html";
            }
         

        } else {
           
 	    if (pxspnum[0].error == 11)
            {  window.location.href = "../../indInfo/trainCourses/trainCourses.html";}
             else
		{
	    alert("请重新登录，培训系统！" + "\n错误提示：" + pxspnum[0].msg);
            window.top.location.href = '../../welcome.html';
		}

        }
  
    }
    function end_tj() {
        $("#zzjz").show();
        s = setTimeout('end_tj()', 500);
        if (myPlayer.currentTime() > 0) {
            $("#zzjz").hide();
            clearTimeout(s);
        }
    }
    function time() {
        getwebdata(timeid, "1", "u_d_contorl", 2);
        setTimeout('time()', 5000);
    }
}
else
{
    alert("请正常登录系统，并在规定的时间内完成学习，谢谢");
    window.top.location.href = '../../welcome.html';
}